package com.dawn.module.cms.controller.app.category;

import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.util.object.BeanUtils;
import com.dawn.module.cms.controller.app.category.vo.AppCategoryListReqVO;
import com.dawn.module.cms.controller.app.category.vo.AppCategoryRespVO;
import com.dawn.module.cms.convert.category.CategoryConvert;
import com.dawn.module.cms.dal.dataobject.category.CategoryDO;
import com.dawn.module.cms.service.category.CmsCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.dawn.framework.common.pojo.CommonResult.success;


@Tag(name = "用户 APP - 类目")
@RestController
@RequestMapping("/cms/category")
@Validated
public class AppCmsCategoryController {

    @Resource
    private CmsCategoryService categoryService;

    @GetMapping("/list")
    @Operation(summary = "获得类目列表")
    public CommonResult<List<AppCategoryRespVO>> getCategoryList(@Valid AppCategoryListReqVO reqVO) {
        List<CategoryDO> list = categoryService.getCategoryList(reqVO);

        List<AppCategoryRespVO> respVOList = CategoryConvert.INSTANCE.convertList03(list);
        // 全部装到Map中
        Map<Long, AppCategoryRespVO> AppCategoryRespVOMap = respVOList.parallelStream()
                .collect(Collectors.toMap(AppCategoryRespVO::getId, a -> a, (k1, k2) -> k1));

        // 将子节点更新到父节点下
        respVOList.forEach(category -> {
            if(category.getParentId().compareTo(0L) != 0) {
                AppCategoryRespVO AppCategoryRespVO = AppCategoryRespVOMap.get(category.getParentId());
                if(CollectionUtils.isEmpty(AppCategoryRespVO.getChildren())) {
                    List<AppCategoryRespVO> children = new ArrayList<AppCategoryRespVO>(){{add(category);}};
                    AppCategoryRespVO.setChildren(children);
                } else {
                    List<AppCategoryRespVO> children = AppCategoryRespVO.getChildren();
                    children.add(category);
//                    children.sort(Comparator.comparingInt(AppCategoryRespVO::getSort));
                    AppCategoryRespVO.setChildren(children);
                }
            }
        });

        // 遍历出第一级父节点
        List<AppCategoryRespVO> parentCategory = respVOList.stream()
                .filter(c -> c.getParentId().compareTo(0L) == 0)
                .collect(Collectors.toList());

        // 只返回第一级节点
        List<AppCategoryRespVO> navAppCategoryRespVOs = new ArrayList<>();
        parentCategory.forEach(pc -> {
            navAppCategoryRespVOs.add(AppCategoryRespVOMap.get(pc.getId()));
        });
        navAppCategoryRespVOs.sort(Comparator.comparingInt(AppCategoryRespVO::getSort));


        return CommonResult.success(categoryService.getCategoryTree(reqVO));
    }


    @GetMapping("/get")
    @Operation(summary = "获得类目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppCategoryRespVO> getArticle(@RequestParam("id") Long id) {
        CategoryDO categoryDO = categoryService.getCategory(id);
        return success(BeanUtils.toBean(categoryDO, AppCategoryRespVO.class));
    }

}
